package org.jeecg.modules.demo.crm.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.C;
import org.jeecg.modules.demo.crm.dto.InvoiceDTO;
import org.jeecg.modules.demo.crm.entity.CCompanyInfoDetails;
import org.jeecg.modules.demo.crm.entity.CInvoiceApplySplit;
import org.jeecg.modules.demo.crm.entity.CInvoiceSplit;
import org.jeecg.modules.demo.crm.entity.CPaymentRecord;
import org.jeecg.modules.demo.crm.mapper.CCompanyInfoDetailsMapper;
import org.jeecg.modules.demo.crm.mapper.CInvoiceApplySplitMapper;
import org.jeecg.modules.demo.crm.mapper.CInvoiceSplitMapper;
import org.jeecg.modules.demo.crm.mapper.CPaymentRecordMapper;
import org.jeecg.modules.demo.crm.service.ICPaymentRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 收款记录
 * @Author: jeecg-boot
 * @Date:   2025-06-20
 * @Version: V1.0
 */
@Service
public class CPaymentRecordServiceImpl extends ServiceImpl<CPaymentRecordMapper, CPaymentRecord> implements ICPaymentRecordService {


    @Autowired
    private CPaymentRecordMapper cPaymentRecordMapper;

    @Autowired
    private CCompanyInfoDetailsMapper cCompanyInfoDetailsMapper;

    @Autowired
    private CInvoiceApplySplitMapper invoiceApplySplitMapper;
    @Override
    @Transactional
    public void add(CPaymentRecord cPaymentRecord) {
        cPaymentRecordMapper.insert(cPaymentRecord);
        //记录总包商
        if(StringUtils.isNotEmpty( cPaymentRecord.getCompanyId())){
            BigDecimal paymentAmount = cPaymentRecord.getPaymentAmount();
            CCompanyInfoDetails companyInfoDetails = new CCompanyInfoDetails();
            companyInfoDetails.setCompanyId(cPaymentRecord.getCompanyId());
            companyInfoDetails.setProjectId(cPaymentRecord.getProjectId());
            companyInfoDetails.setRecordId(cPaymentRecord.getId());
            companyInfoDetails.setAmount(paymentAmount);
            cCompanyInfoDetailsMapper.insert(companyInfoDetails);
        }
        List<InvoiceDTO> invoiceList = cPaymentRecord.getInvoiceApplyList();
        for (InvoiceDTO invoiceDTO : invoiceList) {
            CInvoiceApplySplit split = CInvoiceApplySplit.builder()
                    .invoiceApplyId(invoiceDTO.getId())
                    .paymentRecordId(cPaymentRecord.getId())
                    .amount(invoiceDTO.getAmount())
                    .splitAmount(invoiceDTO.getSplitAmount())
                    .projectId(cPaymentRecord.getProjectId())
                    .companyInfoId(cPaymentRecord.getCompanyId())
                    .build();
            invoiceApplySplitMapper.insert(split);
        }

    }

    @Override
    @Transactional
    public void edit(CPaymentRecord cPaymentRecord) {
        cPaymentRecordMapper.updateById(cPaymentRecord);


        //删除余额记录
        cCompanyInfoDetailsMapper.deleteByRecordId(cPaymentRecord.getId());
        //记录总包商
        if(StringUtils.isNotEmpty(cPaymentRecord.getCompanyId())){
            CCompanyInfoDetails byRecordId = cCompanyInfoDetailsMapper.findByRecordId(cPaymentRecord.getId());
            if(byRecordId == null){
                BigDecimal paymentAmount = cPaymentRecord.getPaymentAmount();
                CCompanyInfoDetails companyInfoDetails = new CCompanyInfoDetails();
                companyInfoDetails.setCompanyId(cPaymentRecord.getCompanyId());
                companyInfoDetails.setProjectId(cPaymentRecord.getProjectId());
                companyInfoDetails.setRecordId(cPaymentRecord.getId());
                companyInfoDetails.setAmount(paymentAmount);
                cCompanyInfoDetailsMapper.insert(companyInfoDetails);
            }
        }
        // 清除记录
        invoiceApplySplitMapper.deleteSplit(cPaymentRecord.getId());
        List<InvoiceDTO> invoiceList = cPaymentRecord.getInvoiceApplyList();
        //记录
        for (InvoiceDTO invoiceDTO : invoiceList) {
            CInvoiceApplySplit split = CInvoiceApplySplit.builder()
                    .invoiceApplyId(invoiceDTO.getId())
                    .paymentRecordId(cPaymentRecord.getId())
                    .amount(invoiceDTO.getAmount())
                    .splitAmount(invoiceDTO.getSplitAmount())
                    .projectId(cPaymentRecord.getProjectId())
                    .companyInfoId(cPaymentRecord.getCompanyId())
                    .build();
            invoiceApplySplitMapper.insert(split);
        }
    }
}
